package solution._0000._0006;

/**
 * Created by chen_snow
 * Date: 2021/5/5 15:38
 * Description: Z 字形变换
 */
public class Solution {
    public String convert(String s, int numRows) {
        if (numRows == 1) {
            return s;
        }

        StringBuilder stringBuilder = new StringBuilder();
        int length = 2 * numRows - 2;
        for (int row = 0; row < numRows; row++) {
            int first = row;
            int second = length - row;

            if (first == second || second - first == length) {
                while (first < s.length()) {
                    stringBuilder.append(s.charAt(first));
                    first += length;
                }
            } else {
                while (second < s.length()) {
                    stringBuilder.append(s.charAt(first));
                    stringBuilder.append(s.charAt(second));
                    first += length;
                    second += length;
                }

                if (first < s.length()) {
                    stringBuilder.append(s.charAt(first));
                }
            }
        }
        return stringBuilder.toString();
    }


}
